<script>
  /* // 1.原型链继承
  function Person() {
    this.name = 'Parent';
  }
  Person.prototype.sayName = function () {
    console.log('sayName', this.name);
  };
  function Child() {
    this.name = 'Child';
  }
  // 子类的显示原型继承父类的实例对象
  Child.prototype = new Person();
  const child = new Child();
  child.sayName(); */

  /*  // 2.构造函数继承
  function Person(name) {
    this.name = name;
    this.colors = ['red', 'green'];
  }
  Person.prototype.sayHello = function () {
    console.log('hello ', this.colors);
  };
  function Child(name) {
    Person.call(this, name);
  }
  const Child1 = new Child('Child1');
  Child1.colors.push('blue');
  const Child2 = new Child('Child2');
  Child1.sayHello(); // 但是无法继承父类的原型属性
  console.log('Child1', Child1);
  console.log('Child2', Child2); */

  /* // 3.组合继承
  function Parent(name) {
    console.log('调用了几次');
    this.name = name;
    this.colors = ['red', 'blue'];
  }
  Parent.prototype.sayName = function () {
    console.log('name为：', this.name);
  };
  function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
  }
  Child.prototype = new Parent();
  // 必须子类的构造器指向子类本身
  Child.prototype.constructor = Child;
  Child.prototype.sayAge = function () {
    console.log('age为：', this.age);
  };

  const child = new Child('张三', 18);
  // const parent = new Parent();
  // console.log('Parent', parent);
  console.log('childdd', child);
  console.log(child.constructor === Child);
  child.sayAge();
  child.sayName(); */

  /*  // 4.寄生组合继承

  function Parent(name) {
    this.name = name;
    this.colors = ['red', 'orange'];
  }

  Parent.prototype.sayName = function () {
    console.log('name:', this.name);
  };

  function Child(name, age) {
    Parent.call(this, name);
    this.age = age;
  }
 //1. 这一步不用Child.prototype = Parent.prototype的原因是怕共享内存，修改父类原型对象就会影响子类
 //2. 不用Child.prototype = new Parent()的原因是会调用2次父类的构造方法（另一次是call），会存在一份多余的父类实例属性
//3. Object.create是创建了父类原型的副本，与父类原型完全隔离
  Child.prototype = Object.create(Parent.prototype);
  Child.prototype.sayAge = function () {
    console.log('age:', this.age);
  };
  Child.prototype.contructor = Child;
  const child = new Child('王五', 20);
  child.sayAge();
  child.sayName();
  // console.log(child instanceof Parent);
  console.log(child); */

  // 5.class继承
  class Parent {
    constructor(name, age) {
      this.name = name;
      this.age = age;
    }
    sayName() {
      console.log('name', this.name);
    }
  }

  class Child extends Parent {
    constructor(name, age) {
      // 必须调用super才能使用this
      super(name); // 相当于ES5中的Parent.call(this,name)
      this.name = name;
      this.age = age;
    }
    sayAge() {
      console.log('age', this.age);
    }
  }

  const child = new Child('李四', 20);
  child.sayAge();
  child.sayName();
</script>
